﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Cit.CDE.Net.Module;
using Cit.CDE.Net.Module.Commons;
using cde.SystemCommon;
using System.Collections.Generic;
using Module;

/// <summary>
/// Copyright (C)   jinggubang
/// FileName:        SingleEdit1.aspx.cs       
/// Author :         gaoyj
/// CDT:             2011-04-01           
/// Version:         1.0.0.0                
/// Depiction:       共通修改1
/// </summary>
public partial class main_main_SingleEdit1 : UserControl.MenuPathPage
{
    protected const string DATA_TABLE = "DATA_TABLE";
    protected CDEEntityManager em;
    protected const string LIST_ENTITY = "LIST_ENTITY";

    protected void Page_Load(object sender, EventArgs e)
    {
        string chkID = string.Empty;
        string fromFlg = string.Empty;
        chkID = (string)Request.QueryString.Get("chkID");
        fromFlg = (string)Request.QueryString.Get("fromFlg");

        //EEntityManager名字
        string EM = Request.QueryString[WEBConstants.EMFN_QUERY_STRING];
        //初始化EM
        if (!Cit.CDE.Net.Module.Commons.CDEConstants.IsEmpty(EM))
        {
            em = cde.cache.CDEFactory.getEMByFullName(EM);
        }
        else
        {
            em = null;
        }

        if (!Page.IsPostBack)
        {
            lblpath.Text = MenuPath + "&gt;" + Resources.Resource.TITLE_EDIT;
            this.hidEnName.Value = em.Full_Name;

            em.Resource_manager = Resources.ResourceErrMsg.ResourceManager;

            List<CDEEntity> newListEntity = new List<CDEEntity>(); ;
            if (chkID != null)
            {
                CDESearchCriteria sc = (CDESearchCriteria)Session[CDEConstants.CONST_SESSION_SC];
                if (sc == null)
                {
                    sc = SearchCriteriaWithXml.CreateSC(em, (Oprator)Session[CDEConstants.SESSION_LOIN_OPRATOR]);
                }
                sc.addCondition("ID", chkID, CDEConstCompare.Include);
                List<CDEEntity> ListEntity = CDEService.LoadEntities(em, sc);

                //edit
                if (fromFlg == "1")
                {
                    //foreach (CDEEntity entity in ListEntity)
                    //{
                    //    entity.Operation = CDEDataOperation.UPDATE;
                    //}

                    //newListEntity = ListEntity;
                    if (ListEntity.Count > 0)
                    {
                        newListEntity.Add(ListEntity[0]);
                        setProperties(newListEntity[0]);
                    }
                    else
                    {
                        newListEntity.Add(EntityWithXml.CreateNewEntity(em, (Oprator)Session[CDEConstants.SESSION_LOIN_OPRATOR]));
                        setProperties(newListEntity[0]);
                    }
                }

                //copy
                //else
                //{
                //    newListEntity = new List<CDEEntity>();
                //    foreach (CDEEntity entity in ListEntity)
                //    {
                //        CDEEntity copy_entity = entity.CopythisWithoutID();
                //        copy_entity.Operation = CDEDataOperation.INSERT;
                //        newListEntity.Add(copy_entity);
                //    }
                //}
            }
            else
            {
                if (em != null)
                {
                    Session[CDEConstants.CONST_SESSION_EM] = em;
                    newListEntity.Add(EntityWithXml.CreateNewEntity(em, (Oprator)Session[CDEConstants.SESSION_LOIN_OPRATOR]));
                    setProperties(newListEntity[0]);
                }
                else
                {
                    Session.Remove(CDEConstants.CONST_SESSION_EM);
                }
            }
            Session[LIST_ENTITY] = newListEntity;
        }
        else
        {
            em = (CDEEntityManager)Session[CDEConstants.CONST_SESSION_EM];
            PostProperties();
        }
    }

    protected void PostProperties()
    {
        dgProperties.DataSource = Session[DATA_TABLE];
        dgProperties.DataBind();
    }

    protected void setProperties(CDEEntity entity)
    {
        dgProperties.entity = entity;

        Session[DATA_TABLE] = dgProperties.DataSource;
    }

    protected void cmdSave_Click(object sender, EventArgs e)
    {
        List<CDEEntity> S_listEntity = (List<CDEEntity>)Session[LIST_ENTITY];
        CDEEntity entity = S_listEntity[0];
        dgProperties.setEntityFromRequest(entity, Request);

        if (CDEConstants.IsEmpty(entity.Id))
        {
            entity.Operation = CDEDataOperation.INSERT;
        }
        else
        {
            entity.Operation = CDEDataOperation.UPDATE; 
        }

        //List<CDEEntity> listEntity = new List<CDEEntity>();
        //entity.Operation = CDEDataOperation.INSERT;
        //listEntity.Add(entity);

        CDEService.UpdateEntities(em, S_listEntity);
        entity = S_listEntity[0];
        setProperties(entity);

        if (entity.Result == CDEDataOperationResult.ERROR)
        {
            base.showMessageErrAndOk(Resources.ResourceErrMsg.SAVE_ERR);
        }
        else
        {
            base.showMessageErrAndOk(Resources.ResourceErrMsg.MSG_SAVE_OK);
        }
    }
}
